<?php

namespace app\controllers;

use app\models\SysMenu;
use app\models\SysRole;
use Yii;
use yii\helpers\Url;

class HomeController extends \yii\web\Controller
{

    public $layout = 'home';

    public function actionIndex()
    {
        if (Yii::$app->user->isGuest) {
            return $this->redirect(Url::to(array('login/index')));
        }


        $m_role = new SysRole();
        $rd_roles = $m_role->findByUserId(Yii::$app->user->getId());

        $sys_admin_flag = false;
        $roleids = [];
        foreach($rd_roles as $role){
            if('sys_admin'== $role['role_code'])
            {
                $sys_admin_flag = true;
                break;
            }
            $roleids[] = $role['roleid'];
        }

        $m_menu = new SysMenu();
        if($sys_admin_flag){
            $rd_menus = $m_menu->getAllMenus();
        }else{
            $rd_menus = $m_menu->getMenusByRoleids($roleids);
        }

        $menu_tree = $m_menu->conventTree($rd_menus,0);
        return $this->render('index',[
            'menu_tree'=>$menu_tree
        ]);
    }


}
